package android.pot.com.smartcar.util;

import android.pot.com.smartcar.R;
import android.pot.com.smartcar.application.MyApplication;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import java.math.BigDecimal;

/**
 * Created by zhaoshilu on 2017/12/20.
 */
public class EditTextUtils {

    /**
     * 设置edittext只能输入小数点后四位，设置btn背景
     */
    public  void afterDotFourSetBg(final EditText editText, final TextView btnview) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().startsWith(".")){
                    return;
                }
                // 限制最多能输入9位整数
                if (s.toString().contains(".")) {
                    if (s.toString().indexOf(".") > 9) {
                        s = s.toString().subSequence(0,9) + s.toString().substring(s.toString().indexOf("."));
                        editText.setText(s);
                        editText.setSelection(9);
                    }
                }else {
                    if (s.toString().length() > 9){
                        s = s.toString().subSequence(0,9);
                        editText.setText(s);
                        editText.setSelection(9);
                    }
                }
                // 判断小数点后只能输入四位
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 4) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 5);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                //如果第一个数字为0，第二个不为点，就不允许输入
                if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void afterTextChanged(Editable s) {
                if (editText.getText().toString().trim() != null && !editText.getText().toString().trim().equals("")) {
                    if (editText.getText().toString().trim().substring(0, 1).equals(".")) {
                        editText.setText("0" + editText.getText().toString().trim());
                        editText.setSelection(2);
                    }else{
                        if (Float.parseFloat(editText.getText().toString())!=0){
                            btnview.setBackgroundResource(R.drawable.selector_black_btn);
                            btnview.setTextColor(ContextCompat.getColor(MyApplication.getContext(),R.color.selector_gray_text));
                            btnview.setEnabled(true);
                        }
                    }
                }else{
                    btnview.setBackgroundResource(R.drawable.corner_gray_bg);
                    btnview.setTextColor(ContextCompat.getColor(MyApplication.getContext(),R.color.main_gray));
                    btnview.setEnabled(false);
                }
            }
        });
    }

    /**
     * 设置edittext只能输入小数点后四位，设置btn背景，金额判断
     */
    public  void afterDotFourSetBg(final EditText editText, final TextView btnview, final String amount) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().startsWith(".")){
                    return;
                }
                // 限制最多能输入9位整数
                if (s.toString().contains(".")) {
                    if (s.toString().indexOf(".") > 9) {
                        s = s.toString().subSequence(0,9) + s.toString().substring(s.toString().indexOf("."));
                        editText.setText(s);
                        editText.setSelection(9);
                    }
                }else {
                    if (s.toString().length() > 9){
                        s = s.toString().subSequence(0,9);
                        editText.setText(s);
                        editText.setSelection(9);
                    }
                }
                // 判断小数点后只能输入四位
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 4) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 5);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                //如果第一个数字为0，第二个不为点，就不允许输入
                if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }

                BigDecimal amountBd = new BigDecimal(amount);
                if (!TextUtils.isEmpty(s.toString())){
                    BigDecimal inputBd = new BigDecimal(s.toString());
                    if (inputBd.compareTo(amountBd)==1){
                        s = s.toString().substring(0,s.length()-1);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void afterTextChanged(Editable s) {
                if (editText.getText().toString().trim() != null && !editText.getText().toString().trim().equals("")) {
                    if (editText.getText().toString().trim().substring(0, 1).equals(".")) {
                        editText.setText("0" + editText.getText().toString().trim());
                        editText.setSelection(2);
                    }else{
                        if (Float.parseFloat(editText.getText().toString())!=0){
                            btnview.setBackgroundResource(R.drawable.selector_black_btn);
                            ((TextView) btnview).setTextColor(ContextCompat.getColor(MyApplication.getContext(),R.color.selector_gray_text));
                            btnview.setEnabled(true);
                        }
                    }
                }else{
                    btnview.setBackgroundResource(R.drawable.corner_gray_bg);
                    ((TextView) btnview).setTextColor(ContextCompat.getColor(MyApplication.getContext(),R.color.main_gray));
                    btnview.setEnabled(false);
                }
            }
        });
    }



}